package LeetCode.month01.day31;

import java.awt.image.AreaAveragingScaleFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;

public class No2271 {
    public static void main(String[] args) {
        System.out.println(maximumWhiteTiles(new int[][]{{20,25},{10,11},{1,5},{12,18},{30,32}},10));
    }
    public static int maximumWhiteTiles(int[][] tiles, int carpetLen) {
        Arrays.sort(tiles,(a,b)->a[0]-b[0]);
        int left = 0;
        int cover = 0;
        int res =0;
        for (int[] tile : tiles) {
            int tr = tile[1];
            int tl = tile[0];
            cover += tr-tl+1;
            int carpetleft = tr-carpetLen+1;
            while(tiles[left][1]<carpetleft){
                cover-=tiles[left][1]-tiles[left][0]+1;
                left++;
            }
            int uncover = Math.max(carpetleft-tiles[left][0],0);
            res = Math.max(res,cover-uncover);
        }
        return res;



//        int n = tiles.length;
//        int len = 0;
//        int left = 0;
//        Arrays.sort(tiles,(a,b)->a[0]-b[0]);
//        int num = 0;
//        long[] arr = new long[tiles[tiles.length-1][1]];
//        for (int i = 0; i < n; i++) {
//            for (int j = tiles[i][0]; j <= tiles[i][1]; j++) {
//                arr[j-1]=1;
//            }
//        }
//
//        for (int i = 0; i < arr.length; i++) {
//            if(arr[i]==1) len++;
//            if(i<carpetLen) continue;
//            if(arr[i-carpetLen]==1) len--;
//            num = Math.max(len,num);
//        }
//        return Math.max(len,num);
    }
}
